package com.suning.sawp.web.controller;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.suning.sawp.dto.oga.bi.StaffDevelopInfo;

/**
 * 总计平均展示逻辑, <br/>
 * 由服务器迭代集合处理页面展示逻辑是很傻逼的行为，但是接口已经定了没办法啊...
 * 
 *
 */
public class StaffDevelopInfoListDecorator extends TotalAvgListDecoraor<StaffDevelopInfo> {

    private List<StaffDevelopInfo> staffDevelopInfos;

    public StaffDevelopInfoListDecorator(List<StaffDevelopInfo> staffDevelopInfos) {
        this.staffDevelopInfos = staffDevelopInfos;
        if (CollectionUtils.isNotEmpty(staffDevelopInfos)) {
            Integer[] arrive = totalavg(
                    Iterables.toArray(Lists.transform(staffDevelopInfos, new Function<StaffDevelopInfo, Integer>() {
                        @Override
                        public Integer apply(StaffDevelopInfo input) {
                            return input.getArrive();
                        }
                    }), Integer.class));
            Integer[] developing = totalavg(
                    Iterables.toArray(Lists.transform(staffDevelopInfos, new Function<StaffDevelopInfo, Integer>() {

                        @Override
                        public Integer apply(StaffDevelopInfo input) {
                            return input.getDeveloping();
                        }
                    }), Integer.class));
            Integer[] follow = totalavg(
                    Iterables.toArray(Lists.transform(staffDevelopInfos, new Function<StaffDevelopInfo, Integer>() {

                        @Override
                        public Integer apply(StaffDevelopInfo input) {
                            return input.getFollow();
                        }
                    }), Integer.class));
            Integer[] recommend = totalavg(
                    Iterables.toArray(Lists.transform(staffDevelopInfos, new Function<StaffDevelopInfo, Integer>() {

                        @Override
                        public Integer apply(StaffDevelopInfo input) {
                            return input.getRecommend();
                        }
                    }), Integer.class));
            StaffDevelopInfo total = new StaffDevelopInfo();
            total.setStaffName("总计");
            StaffDevelopInfo avg = new StaffDevelopInfo();
            avg.setStaffName("平均");
            total.setArrive(arrive[0]);
            avg.setArrive(arrive[1]);
            total.setDeveloping(developing[0]);
            avg.setDeveloping(developing[1]);
            total.setFollow(follow[0]);
            avg.setFollow(follow[1]);
            total.setRecommend(recommend[0]);
            avg.setRecommend(recommend[1]);
            this.staffDevelopInfos = Lists.newArrayList(staffDevelopInfos);
            this.staffDevelopInfos.addAll(0, Lists.newArrayList(total, avg));
        }

    }

    @Override
    protected List<StaffDevelopInfo> delegate() {
        return staffDevelopInfos;
    }

}
